package com.zkingsoft.zkingbase.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zkingsoft.picbroswer.ActivityCode;
import com.zkingsoft.picbroswer.AlbumActivity;
import com.zkingsoft.rxbus.annotation.Subscribe;
import com.zkingsoft.rxbus.annotation.Tag;
import com.zkingsoft.rxbus.thread.EventThread;
import com.zkingsoft.rxbus.thread.ThreadEnforcer;
import com.zkingsoft.zkingbase.R;
import com.zkingsoft.zkingbase.base.BaseActivity;
import com.zkingsoft.zkingbase.base.PermissionHelper;
import com.zkingsoft.zkingbase.bean.User;
import com.zkingsoft.zkingbase.bean.resp.BaseResp;
import com.zkingsoft.zkingbase.constant.Constant;
import com.zkingsoft.zkingbase.manager.GreenDaoManager;
import com.zkingsoft.zkingbase.manager.OkHttpManager;
import com.zkingsoft.zkingbase.utils.PreferenceUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class InfoActivity extends BaseActivity
{
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.circleimg_personal_userhead)
    ImageView circleimgPersonalUserhead;
    @BindView(R.id.rl_person_img)
    RelativeLayout rlPersonImg;
    @BindView(R.id.et_personal_nickname)
    TextView etPersonalNickname;
    @BindView(R.id.rl_nick_name)
    RelativeLayout rlNickName;
    @BindView(R.id.et_personal_realname)
    TextView etPersonalRealname;
    @BindView(R.id.rl_real_name)
    RelativeLayout rlRealName;
    @BindView(R.id.et_personal_idcard)
    TextView etPersonalIdcard;
    @BindView(R.id.rl_id_card)
    RelativeLayout rlIdCard;
    @BindView(R.id.et_personal_phone)
    TextView etPersonalPhone;
    @BindView(R.id.rl_cell_phone)
    RelativeLayout rlCellPhone;
    @BindView(R.id.et_personal_email)
    TextView etPersonalEmail;
    @BindView(R.id.rl_email)
    RelativeLayout rlEmail;
    @BindView(R.id.tv_personal_gender)
    TextView tvPersonalGender;
    @BindView(R.id.rl_gender)
    RelativeLayout rlGender;
    @BindView(R.id.et_personal_area)
    TextView etPersonalArea;
    @BindView(R.id.rl_area)
    RelativeLayout rlArea;
    @BindView(R.id.btn_exit)
    Button btnExit;

    User user;
    OkHttpManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initToolBar();
        manager = OkHttpManager.getInstance();
        getIntentData();
    }

    private void getIntentData()
    {
        user = (User) getIntent().getSerializableExtra("userinfo");
        initUi(user);
    }

    @Override
    public boolean allowTransparentStatus()
    {
        return true;
    }

    public void initUi(User user)
    {
        if(user != null)
        {
            if(!TextUtils.isEmpty(user.suPhoto))
            {
                showImg(Constant.WEB_BASE + user.suPhoto, circleimgPersonalUserhead,false);
            }
            etPersonalNickname.setText(user.suAliases);
            etPersonalRealname.setText(user.suName);
            etPersonalPhone.setText(user.suTel);
            tvPersonalGender.setText(user.suSex);
        }
    }

    /***
     * 配置toolbar
     */
    private void initToolBar()
    {
        toolbar.setNavigationIcon(R.drawable.back_selector);
        toolbar.setNavigationOnClickListener(view -> finish());
        adjustToolbar(toolbar);
        title.setText("个人信息");
        title.setTextColor(getResources().getColor(R.color.white));
        toolbar.setBackgroundColor(getResources().getColor(R.color.DeepOrangeGradeA400));
    }

    @Override
    public int getContentView()
    {
        return R.layout.activity_info;
    }

    @OnClick({R.id.rl_person_img, R.id.rl_nick_name, R.id.rl_real_name,
            R.id.rl_id_card, R.id.rl_cell_phone, R.id.rl_email,
            R.id.rl_gender, R.id.rl_area, R.id.btn_exit})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.rl_person_img:
                startActivityForResult(new Intent(this, AlbumActivity.class), ActivityCode.SELECT_REQUEST);
                break;
            case R.id.rl_nick_name:
                if(user != null)
                {
                    Intent starter = new Intent(this, ModifyNameActivity.class);
                    starter.putExtra("info", user);
                    startActivity(starter);
                }
                break;
            case R.id.rl_real_name:
                break;
            case R.id.rl_id_card:
                break;
            case R.id.rl_cell_phone:
                break;
            case R.id.rl_email:
                break;
            case R.id.rl_gender:
                break;
            case R.id.rl_area:
                break;
            case R.id.btn_exit:
                exit();
                break;
        }
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag("newUser")})
    public void onUpdateAliase(User u)
    {
        if(u != null)
        {
            this.user = u;
            etPersonalNickname.setText(u.suAliases);
        }
    }

    private void exit()
    {
        //标记已经登录
        PreferenceUtils.put(Constant.FLAG_IS_LOGIN, false);
        //记录用户token
        PreferenceUtils.put(Constant.FLAG_TOKEN, "");
        long userId = (long) PreferenceUtils.get(Constant.FlAG_USER_ID, 0l);
        GreenDaoManager m = GreenDaoManager.getInstance(this, Constant.DB_DEFAULT);
        m.getDaoSession().getUserDao().deleteByKey(userId);
        finish();
    }

    private List<String> imgPaths;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(data != null)
        {
            if (requestCode == ActivityCode.REQ_ANIM_IMG_CHOOSE) {
                if (resultCode == ActivityCode.RES_ANIM_IMG_CHOOSE) {
                    imgPaths = (List<String>) data.getSerializableExtra("path");
                    if (imgPaths != null && imgPaths.size() > 0) {
                        toast("图片选择张数 = "+imgPaths.size(), null);
                    }
                }
            }else if(requestCode == ActivityCode.SELECT_REQUEST)    //裁剪图片
            {   //裁剪成功
                if(resultCode == ActivityCode.SELECT_OK_RESULT)
                {
                    //返回裁剪过后的图片路径，一般在外部存储的app缓存目录下面
                    String bmppath = data.getStringExtra("cropimg");
                    if(!TextUtils.isEmpty(bmppath))
                    {
                        showImg(bmppath, circleimgPersonalUserhead,false);
                    }
                    Map<String, String> para = new HashMap<>();
                    para.put("id", String.valueOf(user.suId));
//                    manager.upload(Constant.WEB_BASE + "upload/uploadHeadImg", bmppath, "img",true,para,
//                            new OkHttpManager.ResultCallback<BaseResp>()
//                    {
//                        @Override
//                        public void onError(String simpleMsg, Exception e)
//                        {
//                            Toast.makeText(InfoActivity.this, simpleMsg, Toast.LENGTH_SHORT).show();
//                        }
//
//                        @Override
//                        public void onSuccess(BaseResp response)
//                        {
//                            User u = response.mapInfo.user;
//                            if(u != null)
//                            {
//                                GreenDaoManager d = GreenDaoManager.getInstance(InfoActivity.this, Constant.DB_DEFAULT);
//                                d.getDaoSession().getUserDao().update(u);
//                            }
//                        }
//                    });
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
